package com.sansui.blog.utils;

import com.sansui.blog.pojo.sys.Init;

import java.util.ArrayList;
import java.util.List;

/**
 * @name: TreeUtils
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-03-20 18:30
 **/
public class TreeUtils {

    public static List<Init> toTree(List<Init> treeList, Long pid) {
        List<Init> retList = new ArrayList<>();
        for (Init parent : treeList) {
            if (pid.equals(parent.getPid())) {
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }
    private static Init findChildren(Init parent, List<Init> treeList) {
        for (Init child : treeList) {
            if (parent.getId().equals(child.getPid())) {
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<>());
                }
                parent.getChild().add(findChildren(child, treeList));
            }
        }
        return parent;
    }

}
